
#include "filelist.h"
#include <stdlib.h>
#include <stdio.h>

void fileList_create(FileList** fileList)
{
    (*fileList) = (FileList*) malloc(sizeof(FileList));
    fileList_initialize((*fileList));
}

void fileList_initialize(FileList* fileList)
{
    pthread_mutex_init(&fileList->mutex,NULL);
    list_initialize(&fileList->files);
    list_initialize(&fileList->socket);
}

void fileList_clear(FileList* fileList)
{
    pthread_mutex_lock(&fileList->mutex);
    list_clear(&fileList->files);
    list_clear(&fileList->socket);
    pthread_mutex_unlock(&fileList->mutex);
    pthread_mutex_destroy(&fileList->mutex);
}

void fileList_destroy(FileList** fileList)
{
    fileList_clear((*fileList));
    free((*fileList));
    (*fileList) = NULL;
}

FileState fileList_getFile(FileList* fileList, const char *filename, int socket)
{
    FILE* file = fopen(filename,"rb");
    if(file == NULL)
        return FILESTATE_NOTEXIST;
    else
    {
        char* _filename;
        int _socket;
        Cell *fileCell, *sockCell;
        fclose(file);
        pthread_mutex_lock(&(fileList->mutex));
        fileCell = fileList->files.first;
        sockCell = fileList->socket.first;
        while ((fileCell != NULL) && (sockCell != NULL))
        {
            _filename = (char*) fileCell->value;
            _socket = *((int*) sockCell->value);
            if((_filename == filename) && (_socket != socket))
            {
                pthread_mutex_unlock(&(fileList->mutex));
                return FILESTATE_RESERVED;
            }
            sockCell = sockCell->next;
            fileCell = fileCell->next;
        }
        pthread_mutex_unlock(&(fileList->mutex));
        return FILESTATE_EXIST;
    }
}

int fileList_freeFile(FileList* fileList, const char* filename, int socket)
{
    char* _filename;
    int _socket;
    int ret = 0;
    Cell *fileCell, *sockCell;
    pthread_mutex_lock(&(fileList->mutex));
    fileCell = fileList->files.first;
    sockCell = fileList->socket.first;
    while ((fileCell != NULL) && (sockCell != NULL))
    {
        _filename = (char*) fileCell->value;
        _socket = *((int*) sockCell->value);
        if((_filename == filename) && (_socket == socket))
        {
            list_remove(&fileList->files,fileCell->value);
            list_remove(&fileList->socket,sockCell->value);
            ret = 1;
        }
        sockCell = sockCell->next;
        fileCell = fileCell->next;
    }
    pthread_mutex_unlock(&(fileList->mutex));
    return ret;
}

int fileList_freeClient(FileList* fileList, int socket)
{
    int _socket;
    int ret = 0;
    Cell *fileCell, *sockCell;
    pthread_mutex_lock(&(fileList->mutex));
    fileCell = fileList->files.first;
    sockCell = fileList->socket.first;
    while ((fileCell != NULL) && (sockCell != NULL))
    {
        _socket = *((int*) sockCell->value);
        if(_socket == socket)
        {
            list_remove(&fileList->files,fileCell->value);
            list_remove(&fileList->socket,sockCell->value);
            ret = 1;
        }
        sockCell = sockCell->next;
        fileCell = fileCell->next;
    }
    pthread_mutex_unlock(&(fileList->mutex));
    return ret;
}
